package com.wbdgj.ui.mine;

import android.Manifest;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.view.View;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.internal.LinkedTreeMap;
import com.igexin.sdk.PushManager;
import com.wbdgj.R;
import com.wbdgj.base.BaseActivity;
import com.wbdgj.base.BaseApplication;
import com.wbdgj.http.HttpAdapter;
import com.wbdgj.http.OnResponseListener;
import com.wbdgj.model.ResultObjectModel;
import com.wbdgj.ui.login.LoginActivity;
import com.wbdgj.utils.SpKeyUtils;
import com.wbdgj.utils.ToastUtils;
import com.wbdgj.views.AllRoundImageView;

import java.text.DecimalFormat;
import java.util.ArrayList;

import butterknife.BindView;
import retrofit2.Call;

/**
 * Created by Administrator on 2018/6/1.
 */

public class InformaticaActivity extends BaseActivity{

    @BindView(R.id.name)
    TextView name;

    @BindView(R.id.sex)
    TextView sex;

    @BindView(R.id.card)
    TextView card;

    @BindView(R.id.roundImg)
    AllRoundImageView roundImg;

    @BindView(R.id.idcard)
    TextView idcard;

    @BindView(R.id.phone)
    TextView phone;

    @BindView(R.id.NICKNAME)
    TextView nickname;

    @BindView(R.id.levlel)
    TextView levlel;

    private Context context = this;
    private final int SDK_PERMISSION_REQUEST = 127;

    @Override
    protected int getLayoutResId() {
        return R.layout.ui_information;
    }

    @Override
    protected void initView() {
        getMbUserInfo();
        findViewById(R.id.nicknameLay).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivityForResult(new Intent(context, ChangeNickNameActivity.class), 1);
            }
        });
        findViewById(R.id.ewmLay).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                launch(MyEwmActivity.class);
            }
        });
        findViewById(R.id.sctxLay).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getPersimmions();
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == 1){
            getMbUserInfo();
        }
    }

    private void getMbUserInfo() {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().getMbUserInfo(BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.TOKEN));
        call.enqueue(new OnResponseListener<ResultObjectModel>(context) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
                    LinkedTreeMap linkedTreeMap = (LinkedTreeMap) userModelBaseResultModel.getData();
                    if ((linkedTreeMap.get("NAME")+"").equals("null")){
                    }else{
                        name.setText(linkedTreeMap.get("NAME")+"");
                    }

                    if ((linkedTreeMap.get("PT_CARD")+"").equals("null")){
                        card.setText("");
                    }else{
                        card.setText(""+linkedTreeMap.get("PT_CARD"));
                    }
                    if (linkedTreeMap.containsKey("SEX")){
                        if (decimalFormat.format(linkedTreeMap.get("SEX")).equals("1")){
                            sex.setText("男");
                        }else{
                            sex.setText("女");
                        }
                    }


                    if (linkedTreeMap.containsKey("NICKNAME")){
                        nickname.setText(linkedTreeMap.get("NICKNAME")+"");
                    }else{
                        nickname.setText("请设置");
                    }
                    idcard.setText(linkedTreeMap.get("ID_CARD")+"");
                    card.setText(linkedTreeMap.get("PT_CARD")+"");
                    phone.setText(linkedTreeMap.get("PHONE")+"");
                    if (linkedTreeMap.containsKey("LEVEL")){
                        if (decimalFormat.format(linkedTreeMap.get("LEVEL")).equals("1")){
                            levlel.setText("普通会员");
                        }else if (decimalFormat.format(linkedTreeMap.get("LEVEL")).equals("2")){
                            levlel.setText("黄金会员");
                        }else if (decimalFormat.format(linkedTreeMap.get("LEVEL")).equals("3")){
                            levlel.setText("白金会员");
                        }else if (decimalFormat.format(linkedTreeMap.get("LEVEL")).equals("4")){
                            levlel.setText("钻石会员");
                        }else if (decimalFormat.format(linkedTreeMap.get("LEVEL")).equals("5")){
                            levlel.setText("黑钻会员");
                        }else if (decimalFormat.format(linkedTreeMap.get("LEVEL")).equals("6")){
                            levlel.setText("股东");

                        }else{
                            levlel.setText("普通会员");

                        }
                    }

                    if (!(linkedTreeMap.get("IMG")+"").equals("null")){
                        Glide.with(context).load(HttpAdapter.HEAD_URL+linkedTreeMap.get("IMG")).placeholder(R.mipmap.mine_photo).error(R.mipmap.mine_photo).into(roundImg);
                    }else{
                        Glide.with(context).load("").placeholder(R.mipmap.mine_photo).error(R.mipmap.mine_photo).into(roundImg);
                    }
                }else if (userModelBaseResultModel.getResultCode().equals("1111")){
                    ToastUtils.toastShort("账号没有登录或在其它地方登录");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.TOKEN, "");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.IS_AUTO_LOGIN, "2");
                    PushManager.getInstance().unBindAlias(context, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.LOGIN_PHONE), false, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.CID));
                    Intent intent = new Intent(context, LoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }else{
                    ToastUtils.toastShort(userModelBaseResultModel.getResultMsg());
                }
            }
        });
    }

    @TargetApi(23)
    private void getPersimmions() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            ArrayList<String> permissions = new ArrayList<String>();
            if (addPermission(permissions, Manifest.permission.CAMERA)) {
                permissions.add(Manifest.permission.CAMERA);
            }
            if (addPermission(permissions, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                permissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
            }

            if (permissions.size() > 0) {
                requestPermissions(permissions.toArray(new String[permissions.size()]), SDK_PERMISSION_REQUEST);
            }
        }
    }

    @TargetApi(23)
    private boolean addPermission(ArrayList<String> permissionsList, String permission) {
        if (checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) { // 如果应用没有获得对应权限,则添加到列表中,准备批量申请
            if (shouldShowRequestPermissionRationale(permission)){
                startActivityForResult(new Intent(context, PhotoUploadActivity.class),1);
                return true;
            }else{
                permissionsList.add(permission);
                return false;
            }

        }else{
            startActivityForResult(new Intent(context, PhotoUploadActivity.class),1);
            return true;
        }
    }

    @TargetApi(23)
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        // TODO Auto-generated method stub
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    }
}
